package util.configuration;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.MissingResourceException;
import java.util.Properties;

public class ConfigLoader {
	private static Properties propFile;
	private static ConfigLoader util = new ConfigLoader();
	
	static {
		propFile = new Properties();
		try {
			propFile.load(new FileInputStream("conf.properties"));
		} catch (FileNotFoundException e) {
			//logger.debug(e.getClass().getSimpleName());			
			propFile.setProperty("TransactionTimeout", "10000");
			propFile.setProperty("TransactionNRetrive", "1");
			try {
				propFile.store(new FileWriter(new File("conf.properties")), "configuration file");
			} catch (IOException e1) {
				//logger.debug(e.getClass().getSimpleName());
			}
		} catch (IOException e) {
			//logger.debug(e.getClass().getSimpleName());
		}
	}
	
	/**
	 *
	 * @return
	 */
	public static ConfigLoader access() {
		return util;
	}

	/**
	 *
	 * @param key
	 * @return
	 */
	public static String getString(String key) {
		try {
			return propFile.getProperty(key).toString();
		} catch (MissingResourceException e) {
			return '!' + key + '!';
		}
	}

	
	/**
	 * Numero di tentativi nel caso di fallimento 
	 */
	public final static int TransactionNRetrive = Integer.parseInt(ConfigLoader.getString("TransactionNRetrive"));
	/**
	 * Timeout Transaction
	 */
	public final static int TransactionTimeout = Integer.parseInt(ConfigLoader.getString("TransactionTimeout"));;

}
